<?php

App::uses('AppController', 'Controller');

/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class UsersController extends AppController
{

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator', 'Session');

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('index', 'login', 'logout');
        $this->Auth->autoRedirect = false;
    }

    public function isAuthorized($user)
    {

        if (in_array($this->action, array('edit', 'delete')))
        {
            if ($user['id'] != $this->request->params['pass'][0])
            {
                return false;
            }
        }
        return true;
    }

    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->User->recursive = 0;
        $this->set('users', $this->Paginator->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        if (!$this->User->exists($id))
        {
            throw new NotFoundException(__('Invalid user'));
        }
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->set('user', $this->User->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        if ($this->request->is('post'))
        {
            $this->User->create();
            if ($this->User->save($this->request->data))
            {
                $this->Session->setFlash(__('The user has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        if (!$this->User->exists($id))
        {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is(array('post', 'put')))
        {
            if ($this->User->save($this->request->data))
            {
                $this->Session->setFlash(__('The user has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else
        {
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->request->data = $this->User->find('first', $options);
        }
    }

    public function login()
    {

        //if already logged-in, redirect
        if ($this->Session->check('Auth.User'))
        {
            $this->redirect(array('action' => 'manager'));
        }
        if ($this->request->is('post'))
        {
            if ($this->Auth->login())
            {
                $this->redirect($this->Auth->redirectUrl());
            } else
            {
                $this->Session->setFlash(__('Sai tên đăng nhập hoặc mật khậu!'), 'alert-danger');
            }
        }
    }

    public function logout()
    {
        $this->Session->destroy();
        $this->redirect($this->Auth->redirect());
    }

    public function manager()
    {
        
    }

}
